package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.user.mapper.UserInfoMapper;
import com.originskyseed.platform.user.model.PlayerRelateConsume;
import com.originskyseed.platform.user.model.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.UserCumulativeChargeMapper;
import com.originskyseed.platform.user.model.UserCumulativeCharge;
import com.originskyseed.platform.user.service.IUserCumulativeChargeService;

/**
 * 累充明细表Service接口实现类（OSI_USER_CUMULATIVE_CHARGE）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("userCumulativeChargeService")
public class UserCumulativeChargeServiceImpl implements IUserCumulativeChargeService {

	@Resource
	private UserCumulativeChargeMapper userCumulativeChargeMapper;

	@Resource
	private UserInfoMapper userInfoMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.userCumulativeChargeMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.userCumulativeChargeMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(UserCumulativeCharge record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.userCumulativeChargeMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(UserCumulativeCharge record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.userCumulativeChargeMapper.insertSelective(record);
	}


	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(String tenantCode,UserCumulativeCharge record) throws Exception {
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		UserCumulativeCharge userCumulativeCharge=selectByChannelSubIdAndType(tenantCode,record.getChannelSubId(),record.getType());
		if(userCumulativeCharge==null) {
			record.setCreateTime(new Date());
			return this.userCumulativeChargeMapper.insertSelective(record);
		}
		return 1;
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(UserCumulativeCharge record) throws Exception {
		record.setUpdateTime(new Date());
		return this.userCumulativeChargeMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(UserCumulativeCharge record) throws Exception {
		record.setUpdateTime(new Date());
		return this.userCumulativeChargeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public UserCumulativeCharge selectByPrimaryKey(String id) throws Exception {
		return this.userCumulativeChargeMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询记录,根据渠道id,和累充类型查询
	 *
	 * @param
	 * @return
	 * @throws Exception
	 */
	public UserCumulativeCharge selectByChannelSubIdAndType(String tenantCode,String channelSubId,String type) throws Exception{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.userCumulativeChargeMapper.selectByChannelSubIdAndType(channelSubId,type);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserCumulativeCharge> selectList(UserCumulativeCharge record) throws Exception {
		return this.userCumulativeChargeMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<UserCumulativeCharge> selectListByPage(Map<String, Object> params, BasePage<UserCumulativeCharge> page) throws Exception {
		params.put("page", page);
		List<UserCumulativeCharge> list = this.userCumulativeChargeMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}


	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<UserCumulativeCharge> selectListRelUserByPage(Map<String, Object> params, BasePage<UserCumulativeCharge> page) throws Exception {
		params.put("page", page);
		List<UserCumulativeCharge> list = this.userCumulativeChargeMapper.selectListRelUserByPage(params);

		if(list!=null&&list.size()>0) {
			for(int i=0;i<list.size();i++) {
				UserCumulativeCharge userCumulativeCharge=list.get(i);
				String channelSubId = userCumulativeCharge.getChannelSubId();
				if (channelSubId != null && StringUtils.isNotBlank(channelSubId)) {
					UserInfo userInfo = userInfoMapper.selectByOtherAccount(channelSubId);
					userCumulativeCharge.setName(userInfo == null ? "" : userInfo.getName());
				}
			}
		}
		page.setResults(list);
		return page;
	}


	/**
	 * 查询列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public List<UserCumulativeCharge> selectListRelUserByMap(Map<String, Object> params) throws Exception{
		return this.userCumulativeChargeMapper.selectListRelUserByMap(params);
	}

}